package com.dxf.hchat.netty;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.EventExecutorGroup;

public class HeartBeatHandler  extends ChannelInboundHandlerAdapter {

    /**
     * 用户事件触发
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent){
            IdleStateEvent idleStateEvent= (IdleStateEvent)evt;
            if (idleStateEvent.state()== IdleState.READER_IDLE){
                //读空闲
                System.out.println("触发了读空闲");
            }else if (idleStateEvent.state()==IdleState.WRITER_IDLE){
                //写空闲
                System.out.println("触发了写空闲");
            }else if(idleStateEvent.state()==IdleState.ALL_IDLE){
                //读写空闲
                System.out.println("触发了读写空闲");
                System.out.println("关闭资源通道");
                System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!11");
                ctx.channel().close();
            }

        }
    }
}
